package cn.topcodes.tcsf.admin.commons.api.security;

import cn.topcodes.tcsf.admin.commons.domain.ValueRangeConstant;
import org.apache.shiro.SecurityUtils;

public class ShiroUtil {

	public static Long getUserId() {
		try {
			ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			if (user != null) {
				return user.getUserId();
			}
		}catch (Exception e) {

		}
		return null;
	}
	
	public static String getUsername() {
		try {
			ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			if(user != null) {
				return user.getUsername();
			}
		}catch (Exception e) {

		}
		return null;
	}
	
	public static String getName() {
		try {
			ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			if(user != null) {
				return user.getName();
			}
		}catch (Exception e) {

		}
		return null;
	}
	
	public static ShiroUser getUser() {
		try {
			ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			return user;
		}catch(Exception e) {

		}
		return null;
	}

	public static boolean isDeveloper() {
		return hasRole(ValueRangeConstant.BuiltInRole.ROLE_DEVELOPER);
	}

	public static boolean hasRole(String role) {
		try {
			return SecurityUtils.getSubject().hasRole(role);
		}catch (Exception e) {

		}
		return false;
	}

}
